from django.http import Http404
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
import logging
    
from models import Page, MainPage
from pr_j.models import Project

def main(request, template_name="cm_s/page.html", template_no_main_page="cm_s/no_main_page.html"):
    """
    The main page
    """
    try:
        main_page = MainPage.objects.get()
    except MainPage.DoesNotExist:
        return render_to_response(template_no_main_page, {},
                              context_instance=RequestContext(request))
    try:
        page = Page.published_objects.get(slug=main_page.main_page.slug)
    except Page.DoesNotExist:
        logging.error("invalid pointer to page %s" % main_page.main_page.slug)
        raise Http404
    projects = Project.objects.filter(featured=True)[:3]

    return render_to_response(template_name, {'page':page, 'projects': projects, 'nav_page':'cm_s_main'},
                              context_instance=RequestContext(request))

def page(request, slug, template_name="cm_s/page.html"):
    """
    A page in the CMS
    """
    try:
        page = Page.published_objects.get(slug=slug)
    except Page.DoesNotExist:
        raise Http404
    
    nav_page = "cm_s_%s" % slug
    return render_to_response(template_name, {'page':page, 'nav_page': nav_page,},
                              context_instance=RequestContext(request))
